package com.ruoyi.web.studentmanagementsystem.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.web.studentmanagementsystem.mapper.SysStudentMapper;
import com.ruoyi.web.studentmanagementsystem.pojo.SysStudent;
import com.ruoyi.web.studentmanagementsystem.service.ISysCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: CodeController
 * Package: com.ruoyi.web.studentmanagementsystem.controller
 * Description:
 *
 * @Author Mr. o
 * @Create 2023/11/16 20:22
 * @Version 1.0
 */
@RestController
@RequestMapping("/ISysCodeService")
@Api(tags = "验证码接口相关")
public class CodeController {
    @Autowired
    private ISysCodeService iSysCodeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private SysStudentMapper sysStudentMapper;

    @GetMapping(  "/updateCode")
    @ApiOperation("更新验证码模板")
    public R<String> updateCode() {
        iSysCodeService.updateCode();
        return R.ok();
    }

    @GetMapping(  "/outPutCode")
    @ApiOperation("输出验证码")
    public R<String> outPutCode() {
        return R.ok(iSysCodeService.outPutCode());
    }

    @GetMapping(  "/intPutCode/{studentId}/{answer}")
    @ApiOperation("验证验证码")
    public R<String> inPutCode(@PathVariable String answer, @PathVariable String studentId) {

        List<SysStudent> list = sysStudentMapper.selectStudent();
        //判断用户是否存在
        for (SysStudent l : list) {
            if (l.getStudentId().equals(studentId)) {
                //设置用户有效在线时间：30分钟
                redisTemplate.opsForValue().set("studentId", l.getStudentId(),30, TimeUnit.MINUTES);
                return R.ok(iSysCodeService.inPutCode(answer));
            }
        }

        return R.ok("学生ID输入错误，请重新输入");
    }

    @GetMapping(  "/stateLogOn")
    @ApiOperation("登录状态")
    public R<String> stateLogOn() {
        return R.ok(iSysCodeService.stateLogOn());
    }
}
